package endpoint

import (
	"common/pb"
	"context"
	"go.uber.org/zap"
)

// GetNodeWithDevice 用于调试单个节点
func (s *MetainfoServer) GetNodeWithDevice(ctx context.Context,
	req *pb.GetNodeWithDeviceRequest) (res *pb.GetNodeWithDeviceResponse, err error) {
	devId := string(req.DevId)
	defer func() {
		err = Error.Wrap(err)
		s.log.Info("GetNodeWithDevice", zap.String("devId", devId), zap.Error(err))
	}()

	res = &pb.GetNodeWithDeviceResponse{
		Header:   req.Header,
		NodeInfo: nil,
	}

	_, err = s.validateAuth(ctx, req.Header)
	if err != nil {
		return
	}
	n, err := s.db.FindNodeWithByDevice(ctx, devId)
	if err != nil {
		return
	}
	res.NodeInfo = &pb.NodeInfo{
		NodeId: n.ID.Bytes(),
		StorageNodeAddress: &pb.NodeAddress{
			Ip:       n.Ip,
			Ipv6:     n.Ipv6,
			DeviceId: n.DevId,
			Port:     int32(n.Port),
			Nat:      int32(n.Nat),
		},
		PeerFwd: 0,
	}
	return
}
